/**
 * 
 */
package com.hy.admin.security.config;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hy.admin.domain.dto.AdminWebUserDto;
import com.hy.admin.domain.shared.security.HyUserDetails;
import com.hy.admin.security.utils.MyHttpclientUtils;

/**
 *登陆成功的回调方法
 */
@Component("hyAuthenticationSuccessHandler")
public class HyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

	private Logger logger = LoggerFactory.getLogger(getClass());


	@Value("${admin.username}")
	private String adminUserName;
	
	@Value("${admin.passwd}")
	private String adminPasswd;
	
	@Value("${security.admin.Authorization}")
	private String headerAuthorization;
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.security.web.authentication.
	 * AuthenticationSuccessHandler#onAuthenticationSuccess(javax.servlet.http.
	 * HttpServletRequest, javax.servlet.http.HttpServletResponse,
	 * org.springframework.security.core.Authentication)
	 */
	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
			Authentication authentication) throws IOException, ServletException {

		logger.info("登录成功");
		System.out.println(request.getAttribute("username"));
		ObjectMapper objectMapper=new ObjectMapper();
		AdminWebUserDto adminWebUserDto=null;

		if ("JSON".equals("JSON")) {
			response.setContentType("application/json;charset=UTF-8");
			
			try {
				HyUserDetails hyUserDetails=(HyUserDetails)authentication.getPrincipal();
				Map<String, String> param = new HashMap<>();  
				param.put("grant_type", "password");
				param.put("username",hyUserDetails.getUsername());
				param.put("password", hyUserDetails.getNotpassword());
				String realPath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
				String resp = MyHttpclientUtils.doPost(realPath+"/oauth/token",headerAuthorization, param);
				if (StringUtils.isNotBlank(resp)) {
					adminWebUserDto = objectMapper.readValue(resp, AdminWebUserDto.class);
					adminWebUserDto.getUser().setPassword(null);
				}
				response.getWriter().write(objectMapper.writeValueAsString(adminWebUserDto));
			} catch (Exception e) {
				e.printStackTrace();
				response.getWriter().write(objectMapper.writeValueAsString(e.getMessage()));
			}
			
		} else {
			super.onAuthenticationSuccess(request, response, authentication);
		}

	}

}
